"""PAMR List Genes

.. helpdoc::
List selected PAMR genes.

"""


"""<widgetXML>
    <name>PAMR List Genes</name>
    <icon></icon>
    <tags>
        <tag>PAMR</tag>
    </tags>
    <summary>List selected PAMR genes.</summary>
    <author>
        <authorname>Red-R Core Development Team</authorname>
        <authorcontact>www.red-r.org</authorcontact>
    </author>
    </widgetXML>
"""

"""
<name>PAMR List Genes</name>
<author>Generated using Widget Maker written by Kyle R. Covington</author>
<description></description>
<RFunctions>pamr:pamr.listgenes</RFunctions>
<tags>PAMR</tags>
<icon></icon>
"""
from OWRpy import * 
import redRGUI, signals

class RedRpamr_listgenes(OWRpy): 
    settingsList = []
    def __init__(self,**kwargs):
        OWRpy.__init__(self, **kwargs)
        self.setRvariableNames(["pamr.listgenes"])
        self.require_librarys(["pamr"])
        self.data = {}
        self.RFunctionParam_data = ''
        self.RFunctionParam_fit = ''
        self.RFunctionParam_fitcv = ''
        
        """.. rrsignals::"""
        self.inputs.addInput("data", "PAMR Data Collection (optional)", signals.base.RPAMRData, self.processdata)
        
        """.. rrsignals::"""
        self.inputs.addInput("fit", 'Trained PAMR Model Fit', signals.base.RPAMRFit, self.processfit)
        
        """.. rrsignals::"""
        self.inputs.addInput("fitcv", 'Cross Validated Model Fit (Optional)', signals.base.RPAMRCVFit, self.processfitcv)
        
        """.. rrsignals::"""
        self.outputs.addOutput("pamr.listgenes Output", 'List of Cross Validated Genes', RMatrix)
        
        self.RFunctionParamthreshold_lineEdit = redRLineEdit(self.controlArea, label = "threshold:", text = '0')
        self.RFunctionParamgenenames_checkBox = redRCheckBox(self.controlArea, label = "Use Gene Names?", buttons = ['Yes'])
        redRCommitButton(self.bottomAreaRight, "Commit", callback = self.commitFunction)
        self.RoutputWindow = redRTextEdit(self.controlArea, label = "R Output Window")
    def processdata(self, data):
        
        if data:
            self.RFunctionParam_data=str(data.getData())
            #self.data = data
        else:
            self.RFunctionParam_data=''
    def processfit(self, data):
        
        if data:
            self.RFunctionParam_fit=str(data.getData())
            self.RFunctionParam_data = data.getOptionalData('parent')['data']
            #self.data = data
        else:
            self.RFunctionParam_fit=''
    def processfitcv(self, data):
        
        if data:
            self.RFunctionParam_fitcv=str(data.getData())
            #self.data = data
        else:
            self.RFunctionParam_fitcv=''
    def commitFunction(self):
        if unicode(self.RFunctionParam_data) == '': return
        if unicode(self.RFunctionParam_fit) == '': return
        if unicode(self.RFunctionParam_fitcv) == '': return
        if unicode(self.RFunctionParamthreshold_lineEdit.text()) == '': return
        injection = []
        if unicode(self.RFunctionParamthreshold_lineEdit.text()) != '':
            string = 'threshold='+unicode(self.RFunctionParamthreshold_lineEdit.text())+''
            injection.append(string)
        if 'Yes' in self.RFunctionParamgenenames_checkBox.getChecked():
            injection.append('genenames=TRUE')
        else:
            injection.append('genenames=FALSE')

        inj = ','.join(injection)
        self.R(self.Rvariables['pamr.listgenes']+'<-pamr.listgenes(data='+unicode(self.RFunctionParam_data)+',fit='+unicode(self.RFunctionParam_fit)+',fitcv='+unicode(self.RFunctionParam_fitcv)+','+inj+')')
        self.R('txt<-capture.output('+self.Rvariables['pamr.listgenes']+')')
        self.RoutputWindow.clear()
        tmp = self.R('paste(txt, collapse ="\n")')
        self.RoutputWindow.insertHtml('<br><pre>'+tmp+'</pre>')
        newData = RMatrix(self, data = self.Rvariables["pamr.listgenes"]) # moment of variable creation, no preexisting data set.  To pass forward the data that was received in the input uncomment the next line.
        #newData.copyAllOptinoalData(self.data)  ## note, if you plan to uncomment this please uncomment the call to set self.data in the process statemtn of the data whose attributes you plan to send forward.
        self.rSend("pamr.listgenes Output", newData)
    def getReportText(self, fileDir):
        text = 'This widget lists the genes that survive thresholding from PAMR.\n\n'
        text += 'Threshold = '+unicode(self.RFunctionParamthreshold_lineEdit.text())+'\n\n'
        text += self.RoutputWindow.toPlainText()+'\n\n'
        return text
